import Vue from "vue";
// 监听网络状态
function getIntenet() {
  return new Promise((reslove, reject) => {
    uni.getNetworkType({
      success: function (res) {
        console.log(res.networkType); //网络类型 wifi、2g、3g、4g、ethernet、unknown、none
        if (res.networkType === "none") {
          console.log("当前无网络");
          reslove(false);
        } else {
          console.log("有网络");
          reslove(true);
        }
      },
      fail: function (err) {
        reject(err);
      },
    });
  });
}
function getIntenet2() {
  uni.getNetworkType({
    success: function (res) {
      console.log(res.networkType); //网络类型 wifi、2g、3g、4g、ethernet、unknown、none
      if (res.networkType === "none") {
        console.log("当前无网络");
        Vue.prototype.$intenetState = false;
      } else {
        Vue.prototype.$intenetState = true;
      }
    },
    fail: function (err) {
      Vue.prototype.$intenetState = false;
    },
  });
}
getIntenet2();
function intenetChange() {
  return new Promise((reslove, reject) => {
    uni.onNetworkStatusChange(res => {
      switch (res.networkType) {
        case "none":
          uni.showToast({
            title: "当前无网络，请检查您的网络连接",
            icon: "none",
            duration: 2000,
          });
          Vue.prototype.$intenetState = false;
          break;
        case "2g":
          Vue.prototype.$intenetState = true;
        case "3g":
          Vue.prototype.$intenetState = true;
        case "4g":
          Vue.prototype.$intenetState = true;
        case "5g":
          Vue.prototype.$intenetState = true;
          break;
        case "wifi":
          Vue.prototype.$intenetState = true;
          break;
        default:
          Vue.prototype.$intenetState = true;
          break;
      }
      console.log("当前网络---", res.networkType);
    });
  });
}
Vue.prototype.$intenet = { getIntenet, intenetChange };
